#pragma once
#include <iostream>
#include "protocol.hpp"
enum{
    DIVZERO = 1,
    MODZERO,
    OTHEROPER,
};
class ServerCal
{   
public:
    ServerCal()
    {}
    ~ServerCal()
    {}
public:
    Response CalculatorHelper(const Request &req)
    {
        Response resp(0,0);
        switch (req.op_)
        {
        case '+':
            resp.result_ = req.x_ + req.y_;
            break;
        case '-':
            resp.result_ = req.x_ - req.y_;
            break;
        case '*':
            resp.result_ = req.x_ * req.y_;
            break;
        case '/': 
        {
            if(req.y_ == 0) resp.code_ = DIVZERO; 
            else resp.result_ = req.x_ / req.y_;
        } break;
        case '%':
        {
            if(req.y_ == 0) resp.code_ = MODZERO;
            else resp.result_ = req.x_ % req.y_;
        } break;
        default:
            resp.code_ = OTHEROPER;
            break;
        }
        return resp;
    }
    std::string Calculator(std::string &package)
    {
        std::string content;
        bool ret = Decode(package,&content);
        if(!ret) return "";
        Request req;
        ret = req.Deserialize(content);
        if(!ret) return "";
        Response resp = CalculatorHelper(req);
        content = "";
        resp.Serialize(&content);
        content = Encode(content);
        return content;
    }    
};
